package com.clotingshoppos.modules.product.service.serviceImpl;

import com.clotingshoppos.common.base.BaseService.BaseServiceImpl;
import com.clotingshoppos.modules.product.entity.ProductEntity;
import com.clotingshoppos.modules.product.repository.ProductDDRepository;
import com.clotingshoppos.modules.product.repository.ProductPropertyRepository;
import com.clotingshoppos.modules.product.repository.ProductRepository;
import com.clotingshoppos.modules.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;

/**
 * @ClassName: ProductServiceImp
 * @Description: 产品接口实现类
 * @Author: zhandad
 * @Date: 2019-03-15 09:09
 * @Version: v1.0
 */
@Service
public class ProductServiceImp extends BaseServiceImpl<ProductEntity, ProductRepository> implements ProductService<ProductEntity> {
    @Autowired
    private ProductRepository productRepository;
    @Autowired
    private ProductPropertyRepository productPropertyRepository;
    @Autowired
    private ProductDDRepository productDDRepository;


    /**
    * @Description: 把查询到的产品的属性值id改成具体属性值
    * @Param: [productEntities]
    * @return: java.util.List<com.clotingshoppos.modules.product.entity.ProductEntity>
    * @Author: Izzat
    * @Date: 2019/3/26  22:16
    */
    public List<ProductEntity> idToValue(List<ProductEntity> productEntities) {
        for(ProductEntity e : productEntities){
            this.idToValue(e);
        }
        return productEntities;
    }

    @Override
    /**
    * @Description:  把查询到的产品的属性值id改成具体属性值
    * @Param: [productEntities]
    * @return: com.clotingshoppos.modules.product.entity.ProductEntity
    * @Author: Izzat
    * @Date: 2019/3/26  23:27
    */
    public ProductEntity idToValue(ProductEntity productEntities) {
        productEntities.setColor(productDDRepository.findById(productEntities.getColor()).get().getValue());
        productEntities.setSex(productDDRepository.findById(productEntities.getSex()).get().getValue());
        productEntities.setSize(productDDRepository.findById(productEntities.getSize()).get().getValue());
        productEntities.setType(productDDRepository.findById(productEntities.getType()).get().getValue());
        productEntities.setAge_group(productDDRepository.findById(productEntities.getAge_group()).get().getValue());
        productEntities.setSeason(productDDRepository.findById(productEntities.getSeason()).get().getValue());
        return productEntities;
    }

    @Override
    /**
    * @Description: 把具体属性值转换为她的id
    * @Param: [productEntities]
    * @return: com.clotingshoppos.modules.product.entity.ProductEntity
    * @Author: Izzat
    * @Date: 2019/3/27  9:22
    */
    public String valueToId(String value) {
        return  productDDRepository.findByValue(value).get().getId();
    }

    @Override
    /**
    * @Description: 返回所有产品的列表
    * @Param: []
    * @return: java.util.List<ProductEntity>
    * @Author: zhandad
    * @Date: 2019/3/15  9:49
    */
    public List<ProductEntity> getProductList() {
        List<ProductEntity> list = productRepository.findAll();
        return this.idToValue(list);
    }

    @Override
    /**
    * @Description: 增加一个产品
    * @Param: [productEntity]
    * @return: com.clotingshoppos.modules.product.entity.ProductEntity
    * @Author: zhandad
    * @Date: 2019/3/25  11:14
    */
    public ProductEntity add(ProductEntity productEntity) {
        return this.idToValue(productRepository.save(productEntity));
    }


    /**
    * @Description: 按id查找产品
    * @Param: [productEntity]
    * @return: com.clotingshoppos.modules.product.entity.ProductEntity
    * @Author: zhandad
    * @Date: 2019/3/25  11:15
    */
    public ProductEntity findById(ProductEntity productEntity) {
        ProductEntity entity = findById(productEntity.getId());
        return this.idToValue(entity);
    }


    @Override
    /**
    * @Description: 按产品类型查找产品
    * @Param: [type] 产品类型编号
    * @return: java.util.List<ProductEntity>
    * @Author: zhandad
    * @Date: 2019/3/15  9:50
    */
    public List<ProductEntity> findByType(String type) {
        List<ProductEntity> list = productRepository.findByType(this.valueToId(type));
        return this.idToValue(list);
    }

    @Override
    /**
    * @Description: 按照产品适用性别查找
    * @Param: [sex] 性别编号
    * @return: java.util.List<ProductEntity>
    * @Author: zhandad
    * @Date: 2019/3/15  9:53
    */
    public List<ProductEntity> findBySex(String sex) {
        List<ProductEntity> list = productRepository.findBySex(this.valueToId(sex));
        return this.idToValue(list);
    }

    @Override
    /**
    * @Description: 按照产品名称查找
    * @Param: [name] 产品名称
    * @return: java.util.List<com.clotingshoppos.modules.product.entity.ProductEntity>
    * @Author: zhandad
    * @Date: 2019/3/20  22:25
    */
    public List<ProductEntity> findByName(String name) {
        List<ProductEntity> list = productRepository.findByName(name);
        return this.idToValue(list);
    }

    @Override
    /**
    * @Description: 按照产品名和尺码查找产品
    * @Param: [name, size] 产品名，性别编号
    * @return: java.util.List<ProductEntity>
    * @Author: zhandad
    * @Date: 2019/3/15  9:56
    */
    public List<ProductEntity> findByNameAndSize(String name, String size) {
        List<ProductEntity> list = productRepository.findByNameAndSize(name,this.valueToId(size));
        return this.idToValue(list);    }

    @Override
    /**
    * @Description: 按照产品名和配色查找产品
    * @Param: [name, color] 产品名，颜色
    * @return: java.util.List<ProductEntity>
    * @Author: zhandad
    * @Date: 2019/3/15  9:57
    */
    public List<ProductEntity> findByNameAndColor(String name, String color) {
        List<ProductEntity> list = productRepository.findByNameAndColor(name,this.valueToId(color));
        return this.idToValue(list);
    }

    @Override
    /**
    * @Description: 按照产品名，尺码和配色查询
    * @Param: [name, size, color] 产品名，尺码，颜色
    * @return: ProductEntity
    * @Author: zhandad
    * @Date: 2019/3/15  9:58
    */
    public ProductEntity findByNameAndSizeAndColor(String name, String size, String color) {
        ProductEntity entity = productRepository.findByNameAndSizeAndColor(name,this.valueToId(size),this.valueToId(color
        ));
        return this.idToValue(entity);
    }

}
